// page-deviceImg/updateDeviceImg/updateDeviceImg.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: app.globalData.requestURL +'upload/uploadImgTest',
    path: '',//文件本地路径
    name: '',//name文件名
    imagesType: '',//自定义上传的目录路径
    
    update_zuoqianfang: wx.getStorageSync('update_zuoqianfang'),
    update_youqianfang: wx.getStorageSync('update_youqianfang'),
    update_youhoufang: wx.getStorageSync('update_youhoufang'),
    update_zuohoufang: wx.getStorageSync('update_zuohoufang'),
    
    bg_zuoqianfang: '',
    bg_youqianfang: '',
    bg_youhoufang: '',
    bg_zuohoufang: '',
    bigShelfNum: '',
    qylxr: false,
    isSubmit: true,
    rollOutIdentification:''//转出人
  },
  
  chooseImage: function (e) {
    var self = this;
    wx.chooseImage({
      count: 1,
      sourceType: ['camera'],
      success: function (res) {
        if (e.currentTarget.id == '1') {
          self.setData({
            name: "zuoqianfang",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.identityNum + '/' + self.data.bigShelfNum + '/' + "waiguan"
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('update_zuoqianfang', imgResponseUrl)
            self.setData({
              update_zuoqianfang: wx.getStorageSync('update_zuoqianfang')
            })
            if (self.data.update_zuoqianfang != '') {
              self.setData({
                bg_zuoqianfang: ''
              })
            }
          };
        } else if (e.currentTarget.id == '2') {
          self.setData({
            name: "youqianfang",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.identityNum + '/' + self.data.bigShelfNum + '/' + "waiguan"
          })
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('update_youqianfang', imgResponseUrl)
            self.setData({
              youqianfang: wx.getStorageSync('update_youqianfang')
            })
            if (self.data.update_youqianfang != '') {
              self.setData({
                bg_youqianfang: ''
              })
            }
          };
        } else if (e.currentTarget.id == '3') {
          self.setData({
            name: "youhoufang",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.identityNum+'/' + self.data.bigShelfNum + '/' + "waiguan"
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('update_youhoufang', imgResponseUrl)
            self.setData({
              update_youhoufang: wx.getStorageSync('update_youhoufang')
            })
            if (self.data.update_youhoufang != '') {
              self.setData({
                bg_youhoufang: ''
              })
            }
          };
        } else if (e.currentTarget.id == '4') {
          self.setData({
            name: "zuohoufang",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.identityNum + '/' + self.data.bigShelfNum + '/' + "waiguan"
          })
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('update_zuohoufang', imgResponseUrl)
            self.setData({
              update_zuohoufang: wx.getStorageSync('update_zuohoufang')
            })
            if (self.data.zuohoufang != '') {
              self.setData({
                bg_zuohoufang: ''
              })
            }
          };
        }
      }
    });
  },
  submit: function (e) {
    var that = this;
    if (that.data.isSubmit) {
      that.setData({
        isSubmit: false
      })
      var bigShelfNum = that.data.bigShelfNum;//设备编号
      var deviceFrontRightImg = that.data.update_youqianfang;
      if (deviceFrontRightImg == "" || deviceFrontRightImg == null) {
        wx.showToast({
          title: '照片错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        return false;
      }
      var deviceFrontLiftImg = that.data.update_zuoqianfang;
      if (deviceFrontLiftImg == "" || deviceFrontLiftImg == null) {
        wx.showToast({
          title: '照片错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        return false;
      }
      var deviceLiftAfterImg = that.data.update_zuohoufang;
      if (deviceLiftAfterImg == "" || deviceLiftAfterImg == null) {
        wx.showToast({
          title: '照片错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        return false;
      }
      var deviceRightAfterImg = that.data.update_youhoufang;
      if (deviceRightAfterImg == "" || deviceRightAfterImg == null) {
        wx.showToast({
          title: '照片错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        return false;
      }
      wx.request({
        url: app.globalData.requestURL +'deviceImg/save',
        method: 'POST',
        data: {
          bigShelfNum: bigShelfNum, //设备编号
          deviceFrontRightImg: deviceFrontRightImg, //设备右前方
          deviceFrontLiftImg: deviceFrontLiftImg,//设备左前方
          deviceLiftAfterImg: deviceLiftAfterImg,//设备左后方
          deviceRightAfterImg: deviceRightAfterImg//设备右后方
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          console.log(res.data)
          if (res.data.status == '0') {
            wx.setStorageSync('update_zuoqianfang', '');
            wx.setStorageSync('update_youqianfang', '');
            wx.setStorageSync('update_youhoufang', '');
            wx.setStorageSync('update_zuohoufang', '');
            wx.showToast({
              title: '设备添加成功',
              icon: 'success',
              duration: 2000,
              mask: true,
              success: function () {
                wx.redirectTo({
                  url: '/page-billOfDocumentImg/updateBillOfDocumentImg/updateBillOfDocumentImg'
                })
              }
            });
          } else {
            wx.showToast({
              title: '设备添加失败',
              icon: 'loading',
              duration: 2000,
              mask: true,
              success: function () {
                that.setData({
                  isSubmit: true
                })
              }
            });
          }
        }
      })
    } else {
      wx.showToast({
        title: '不可重复提交',
        icon: 'loading',
        duration: 2000,
        mask: true
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      bigShelfNum: wx.getStorageSync("rollOutFactoryNum"),//转出设备的编号
      identityNum: wx.getStorageSync('identification'),//转入单位
      rollOutIdentification: wx.getStorageSync("rollOutIdentification")//转出单位
    });
    var bigShelfNum = that.data.bigShelfNum
    var identityNum = that.data.identityNum
    var rollOutIdentification = that.data.rollOutIdentification

    //查询此照片表最近更新的照片
    wx.request({
      url: app.globalData.requestURL + 'deviceImg/getDeviceImgByCondition',
      method: 'POST',
      data: {
        bigShelfNum: bigShelfNum,
        identityNum: identityNum
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //如果有
        if (res.data.status == '1') {
          wx.redirectTo({
            url: '/page-billOfDocumentImg/updateBillOfDocumentImg/updateBillOfDocumentImg'
          })
        } if (res.data.status == '0'){
          wx.request({
            url: app.globalData.requestURL +'deviceImg/getNewDeviceImgByEntity',
            method: 'POST',
            data: {
              bigShelfNum: bigShelfNum,
              identityNum: rollOutIdentification
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res2) {
              //如果有
              if (res2.data.status == '0') {
                that.setData({
                  update_zuoqianfang: res2.data.data[0].deviceFrontLiftImg,
                  update_youqianfang: res2.data.data[0].deviceFrontRightImg,
                  update_youhoufang: res2.data.data[0].deviceRightAfterImg,
                  update_zuohoufang: res2.data.data[0].deviceLiftAfterImg
                })
              }
            }
          })
        }
      }
    })
    if (wx.setStorageSync('update_zuoqianfang', '') != '') {
      that.setData({
        bg_zuoqianfang: ''
      });
    }
    if (wx.setStorageSync('update_youqianfang', '') != '') {
      that.setData({
        bg_youqianfang: ''
      });
    }
    if (wx.getStorageSync("update_youhoufang") != '') {
      that.setData({
        bg_youhoufang: ''
      });
    }
    if (wx.getStorageSync("update_zuohoufang") != '') {
      that.setData({
        bg_zuohoufang: ''
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})